using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml;

using vs.Core;

namespace Forum
{
    public partial class avatar : System.Web.UI.Page
    {
        public string GetExtension(string file)
        {
            string[] s = file.Split('.');
            if (s.Length > 1)
                return s[s.Length - 1];
            return "";
        }

        public bool IsImage(string file)
        {
            string ext = GetExtension(file);
            ext = ext.ToLower();
            if (ext == "gif" || ext == "jpg" || ext == "jpeg")
                return true;
            return false;
        }

        public string GetFileName(string file, char spliter)
        {
            string[] s = file.Split(spliter);
            if (s.Length > 1)
                return s[s.Length - 1];
            return "";
        }

        private XmlDocument GenXML(ArrayList files, string related_path)
        {
            XmlDocument doc = new XmlDocument();
            doc.LoadXml("<galery></galery>");
            XmlElement root = doc.DocumentElement;

            XmlElement row = doc.CreateElement("row");
            for (int i = 0; i < files.Count; i++)
            {
                string path = ResolveUrl(related_path + files[i].ToString());
                string js = "javascript:doSelect('" + path + "');";
                string del_url = "ImageGalery.aspx?action=ImageFileDel&id=" + path;

                XmlElement img = doc.CreateElement("img");
                CommonFunction.AddNode(doc, img, "imgname", files[i].ToString());
                CommonFunction.AddNode(doc, img, "imgpath", path);
                CommonFunction.AddNode(doc, img, "js", js);
                row.AppendChild(img);

                if ((i + 1) % 4 == 0)
                {
                    root.AppendChild(row);
                    row = doc.CreateElement("row");
                }

            }
            root.AppendChild(row);

            return doc;
        }


        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (!Page.IsPostBack)
                {
                    string related_path = UrlProvider.RootFolder + "/images/avatars/";
                    string path = Server.MapPath(related_path);
                    string[] files = System.IO.Directory.GetFiles(path);
                    ArrayList fs = new ArrayList();
                    for (int i = 0; i < files.Length; i++)
                        if (IsImage(files[i]))
                        {
                            string fname = GetFileName(files[i], '\\');
                            fs.Add(fname);
                        }

                    XmlDocument doc = GenXML(fs, related_path);
                    string template = ResolveUrl("~/Desktops/Forum/Template/avatars.xsl");
                    CommonFunction.XMLTransform(xmlTransformer, template, doc);
                }
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }
        }
    }
}
